package net.ibizsys.eclipse.studio.vc.svn;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;

import net.ibizsys.eclipse.studio.vc.IVCActionListener;
import net.ibizsys.eclipse.studio.vc.IVCManager;
import net.ibizsys.eclipse.studio.vc.VCRepository;

import org.tigris.subversion.javahl.ClientException;
import org.tigris.subversion.javahl.Notify2;
import org.tigris.subversion.javahl.NotifyInformation;
import org.tigris.subversion.javahl.Revision;
import org.tigris.subversion.javahl.SVNClient;

/**
 * SVN版本控制管理对象
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class SVNManager implements IVCManager
{
	private SVNClient createClient(VCRepository repository, final IVCActionListener listener)
	{
		SVNClient client = new SVNClient();
		client.username(repository.getUsername());
		client.password(repository.getPassword());

		client.notification2(new Notify2()
		{
			@Override
			public void onNotify(NotifyInformation a)
			{
				String info = SVNNotifyAction.GetActionText(a.getAction()) + "[" + a.getPath() + "]";

				if (listener != null)
				{
					listener.onNotify(info);
				}
			}
		});

		return client;
	}

	@Override
	public void ignore(VCRepository repository, String path, IVCActionListener listener) throws ClientException
	{
		SVNClient client = createClient(repository, listener);

		File file = new File(path);
		if(file.exists())
		{
			client.propertySet(path, "svn:ignore", ".", true);	
		}
	}

	@Override
	public void checkout(VCRepository repository, String module, String destPath, IVCActionListener listener) throws ClientException
	{
		SVNClient client = createClient(repository, listener);

		File file = new File(destPath);

		if (!file.exists())
		{
			file.mkdirs();
		}

		if (module == null)
		{
			module = "";
		}
		client.checkout(repository.getUrl() + "/" + module, destPath, org.tigris.subversion.javahl.Revision.HEAD, true);

		try
		{
			Files.setAttribute(FileSystems.getDefault().getPath(destPath, ".svn"), "dos:hidden", true);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void update(VCRepository repository, String destpath, IVCActionListener listener) throws ClientException
	{
		SVNClient client = createClient(repository, listener);

		client.update(destpath, Revision.HEAD, true);
	}

	@Override
	public void update(VCRepository repository, String[] destpath, IVCActionListener listener) throws ClientException
	{
		SVNClient client = createClient(repository, listener);

		client.update(destpath, Revision.HEAD, true, true);
	}

	@Override
	public void commit(VCRepository repository, String destpath, String message, IVCActionListener listener) throws ClientException
	{
		SVNClient client = createClient(repository, listener);

		String[] dests = { destpath };

		client.commit(dests, message, true, true);
	}

	@Override
	public void commit(VCRepository repository, String[] destpath, String message, IVCActionListener listener) throws ClientException
	{
		SVNClient client = createClient(repository, listener);

		client.commit(destpath, message, true, true);
	}
}
